package saolei;

import java.io.File;
import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class MyLogger {
    public static Logger logger = Logger.getLogger("com.itheima.log.JULTest");
    static {
        // 自定义可以输出所有级别的信息，注意修改日志文件路径，用记事本的编码是正确的
        logger.setUseParentHandlers(false);
        ConsoleHandler consoleHandler = new ConsoleHandler();
        SimpleFormatter simpleFormatter = new SimpleFormatter();
        consoleHandler.setFormatter(simpleFormatter);
        logger.addHandler(consoleHandler);
        logger.setLevel(Level.ALL);
        consoleHandler.setLevel(Level.ALL);
        FileHandler fileHandler;
        try {
            File file = new File("././log/testlog.txt");
            if (!file.exists())
                file.createNewFile();
            fileHandler = new FileHandler("././log/testlog.txt");
            fileHandler.setFormatter(simpleFormatter);
            logger.addHandler(fileHandler);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}